package com.ternnetwork.core.controller;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.ternnetwork.commons.model.http.BaseResponse;
import com.ternnetwork.commons.utils.PrintWriterUtils;
import com.ternnetwork.commons.utils.ResponseContentType;
import com.ternnetwork.core.entity.Role;
import com.ternnetwork.core.entity.User;
import com.ternnetwork.core.service.RoleService;
import com.ternnetwork.core.service.UserService;
import com.ternnetwork.persistence.page.Page;




@Controller
@RequestMapping("/core/user/*")
public class UserController {
	
	@Resource
	private UserService userService;

	@Resource
	private RoleService roleService ;
	
	
	private static final Logger log = Logger.getLogger(UserController.class);
	
	@RequestMapping("manage.htm")
	public ModelAndView manage(){
		List<Role> list=roleService.findAll("from Role",null);
		return new ModelAndView("/WEB-INF/view/core/user_manage.jsp").addObject("roleList",list);
	}
	
	@RequestMapping("passwordUpdate.htm")
	public ModelAndView passwordUpdate(){
		return new ModelAndView("/WEB-INF/view/core/password_update.jsp");
	}
	
	@RequestMapping("passwordUpdateForExpired.htm")
	public ModelAndView passwordUpdateForExpired(String username){
		return new ModelAndView("/WEB-INF/view/core/password_update_for_expired.jsp").addObject("username",username);
	}
	
	@RequestMapping("infoUpdate.htm")
	public ModelAndView userInfoUpdate(HttpServletRequest request) throws NoSuchAlgorithmException{
		User user=userService.getCurrentUser();
		ModelAndView mv=new ModelAndView("/WEB-INF/view/core/user_info_update.jsp").addObject("user",user);
		return mv;
	}
	
	@RequestMapping("query.htm")
	public void query(PrintWriter out,HttpServletResponse response,String sort,String order,int limit, int offset,User t){
		Page page=Page.createPage(limit, offset);
		PrintWriterUtils.printString(out, response, userService.queryToJsonStr(t, page));
	}
	
	
	
	@RequestMapping("add.htm")
	public @ResponseBody BaseResponse add(User t){
	    return userService.idoAdd(t);
	}
	
	
	@RequestMapping("update.htm")
	public @ResponseBody BaseResponse update(User t){
	   return userService.idoUpdate(t);
	}
	
	
	@RequestMapping("updateInfo.htm")
	public @ResponseBody BaseResponse updateUserInfo(User t){
		return userService.idoUpdateUserInfo(t);
	}
	
	@RequestMapping("updatePassword.htm")
	public @ResponseBody BaseResponse updatePassword(String orinPassword,String newPassword){
	   return userService.idoUpdatePassword(orinPassword, newPassword);
	}
	
	@RequestMapping("updatePasswordForExpired.htm")
	public @ResponseBody BaseResponse updatePasswordForExpired(String username,String orinPassword,String newPassword){
	   return userService.idoUpdatePassword(username,orinPassword, newPassword);
	}
	
	@RequestMapping("findByWechatIdJSONP.htm")
	public void findByWechatId(PrintWriter out,HttpServletResponse response,String jsonpcallback,String wechatId){
		String result=userService.findWechatId(wechatId).toString();
		PrintWriterUtils.printString(out, response,jsonpcallback+"("+result+")");
	}
	
	
	@RequestMapping("delete.htm")
	public @ResponseBody BaseResponse delete(long id){
	   return userService.idoDelete(id);
	}
	
	@RequestMapping("resetPassword.htm")
	public @ResponseBody BaseResponse resetPassword(String userName, String email){
	   return userService.idoResetPassword(userName, email);
	}
	
	@RequestMapping("getCurrentUser.htm")
	public void getCurrentUser(PrintWriter out,HttpServletResponse response) {
		User user=userService.getCurrentUser();
		PrintWriterUtils.printString(out, response,user.toString());
	}

	@RequestMapping("exportExcel.htm")
	public void exportExcel(HttpServletResponse response,User t) {
		        try {
			        
					OutputStream  os= response.getOutputStream();
		            response.reset();
					String contentType=ResponseContentType.getContentTypeByFileExtendsName("xls");
					response.setHeader("Content-Disposition","attachment; filename=user.xls");
					response.setContentType(contentType);
					userService.createExcel(t).write(os);
					os.flush();
					os.close();  
				} catch (Exception e) {
						log.error("[用户导出异常]",e);
				}
	}

}
